/**
 * I18nMessageTag.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.codehaus.plexus.util.StringUtils;

import com.site.util.extremetable.QueryPage;


/**
 * 翻页标签
 */
public class PageTag extends TagSupport {


	private static final long serialVersionUID = -8963957071950556995L;

	
	private String action;

	private String namespace;

	private Map<String,String> paramMap=new HashMap<String,String>();
	private QueryPage queryPage;
	@Override
	public int doStartTag() throws JspException {
		queryPage=(QueryPage)pageContext.getRequest().getAttribute("queryPage");
		paramMap.clear();
		
		return EVAL_BODY_INCLUDE;
	}
	

	@Override
	public int doEndTag() throws JspException {
		try {
				
				if(queryPage == null || queryPage.getPageCount()==0)
				{
					return super.doEndTag();
				}
				
				String path=getActionPath();
				pageContext.getOut().write("<form action=\""+path+"\" method=\"post\" id=\"pageTurnId\" onsubmit=\"return checkTargetPage(targetPage.value);\">");
				
				pageContext.getOut().write("&nbsp;共"+queryPage.getPageCount()+"页 "+queryPage.getRecordCount()+"条记录&nbsp;");
				
				for(Map.Entry<String,String> param:paramMap.entrySet())
				{
					pageContext.getOut().write("<input type=\"hidden\" name=\""+param.getKey()+"\" value=\""+param.getValue()+"\">");
				}
				pageContext.getOut().write("<input id=\"targetPage\" type=\"text\"  style=\"width:30px;text-align: center;\" name=\"queryPage.targetPage\" value=\""+queryPage.getTargetPage()+"\">");
				pageContext.getOut().write("&nbsp;<input class=\"confirm\" type=\"submit\" class=\"simButton\" value=\"转到\">");
				if(queryPage.getTargetPage()>1)
				{
					pageContext.getOut().write("&nbsp;<a class=\"next\" href=\""+getFirstPagePath()+"\"/>首页</a>&nbsp;");
				}
				else
				{
					pageContext.getOut().write("&nbsp;<span class=\"rankecss\">首页</span>&nbsp;");
				}
				if(queryPage.getTargetPage()>1)
				{
					pageContext.getOut().write("<a class=\"next\" href=\""+getPreviousPagePath()+"\"/>上一页</a>&nbsp;");
				}
				else
				{
					pageContext.getOut().write("<span class=\"rankecss\">上一页</span>&nbsp;");
				}
				if(queryPage.getTargetPage()<queryPage.getPageCount())
				{
					pageContext.getOut().write("<a class=\"next\" href=\""+getNextPagePath()+"\"/>下一页</a>&nbsp;");
				}
				else
				{
					pageContext.getOut().write("<span class=\"rankecss\">下一页</span>&nbsp;");
				}
				if(queryPage.getTargetPage()<queryPage.getPageCount())
				{
					pageContext.getOut().write("<a class=\"next\" href=\""+getLastPagePath()+"\"/>末页</a>&nbsp;");
				}
				else
				{
					pageContext.getOut().write("<span class=\"rankecss\">末页</span>&nbsp;");
				}
				pageContext.getOut().write(getCheckFunctionJS());
				pageContext.getOut().write("</form>");
				

		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("翻页标签出错！");
		}
		return super.doEndTag();
	}


	private String getFirstPagePath()
	{
		String path=getActionPath();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 开始
		StringBuffer path2=new StringBuffer();
		path2.append("?queryPage.targetPage="+1);
		for(Map.Entry<String,String> param:paramMap.entrySet())
		{
			if(!StringUtils.isEmpty(param.getKey())&&!StringUtils.isEmpty(param.getValue()))
			{
				try {
					path2.append("&"+param.getKey()+"="+java.net.URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path+=path2.toString();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 结束
		return path;
	}
	
	private String getLastPagePath()
	{
		String path=getActionPath();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 开始
		StringBuffer path2=new StringBuffer();
		path2.append("?queryPage.targetPage="+queryPage.getPageCount());
		for(Map.Entry<String,String> param:paramMap.entrySet())
		{
			if(!StringUtils.isEmpty(param.getKey())&&!StringUtils.isEmpty(param.getValue()))
			{
				try {
					path2.append("&"+param.getKey()+"="+java.net.URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path+=path2.toString();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 结束
		return path;
	}
	
	private String getNextPagePath()
	{
		String path=getActionPath();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 开始
		StringBuffer path2=new StringBuffer();
		path2.append("?queryPage.targetPage="+(queryPage.getTargetPage()+1));
		for(Map.Entry<String,String> param:paramMap.entrySet())
		{
			if(!StringUtils.isEmpty(param.getKey())&&!StringUtils.isEmpty(param.getValue()))
			{
				try {
					path2.append("&"+param.getKey()+"="+java.net.URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path+=path2.toString();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 结束
		return path;
	}

	private String getPreviousPagePath()
	{
		String path=getActionPath();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 开始
		StringBuffer path2=new StringBuffer();
		path2.append("?queryPage.targetPage="+(queryPage.getTargetPage()-1));
		for(Map.Entry<String,String> param:paramMap.entrySet())
		{
			if(!StringUtils.isEmpty(param.getKey())&&!StringUtils.isEmpty(param.getValue()))
			{
				try {
					path2.append("&"+param.getKey()+"="+java.net.URLEncoder.encode(param.getValue(),"UTF-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		path+=path2.toString();
		//将path+="" 换成 path2.append("") qyq 2011-08-1-27 结束
		return path;
	}
	
	private String getActionPath()
	{
		String path=pageContext.getServletContext().getContextPath()+namespace+"/"+action+".do";
		return path;
	}
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public Map<String,String> getParamMap() {
		return paramMap;
	}


	public void setParamMap(Map<String,String> paramMap) {
		this.paramMap = paramMap;
	}
	
	private String getCheckFunctionJS()
	{
		String checkJS="<script  type=\"text/javascript\">" +
				"function checkTargetPage(targetPage)" +
				"{" +
				"	var r1= /^[1-9]\\d*$/;	" +
				"	if(!r1.test(targetPage))	" +
				"	{		" +
				"		alert(\"请输入正整数\");	" +
				"		return false;" +
				"	}" +
				"	else if(targetPage>"+queryPage.getPageCount()+")" +
				"	{" +
				"		alert(\"超过最大页数\");" +
				"		return false;" +
				"	}" +
				"	return true;" +
				"}" +
				"</script>";
		return checkJS;
	}

}
